/*
 * ContextProperty.java
 *
 * Created on 14 de diciembre de 2004, 17:47
 */

package ocp.context;

import java.util.Vector;
import java.util.Enumeration;

/**
 * This class represents a property object in our context. This objects are properties that link
 * several classes. I.e: the property hasChild links two "Person" classes, pedro and jose are
 * two instances of Person. Every ContextProperty follows a ContextPropertyDefinition.
 * @author  nacho
 */
public class ContextProperty implements java.io.Serializable  {

    protected ContextPropertyDefinition propertyDefinition;
    
    /** contains ContextualInformation */
    protected Vector domains;
    
    /** contains ContextualInformation */
    protected Vector ranges;

    /** Creates a new instance of ContextProperty */
    public ContextProperty(ContextPropertyDefinition cpd,ContextualInformation [] d,ContextualInformation [] r) 
        throws ContextException {
            propertyDefinition = cpd;
            this.domains = new Vector();
            for (int i = 0; i < d.length; i++)
                this.domains.addElement(d[i]);
            this.ranges = new Vector();
            for (int i = 0; i < r.length; i++)
                this.ranges.addElement(r[i]);
            if (!cpd.validate(this))
                throw new ContextException("Property doesn't match context property definition");
    }
    
    /** Creates a new instance of ContextProperty */
    public ContextProperty(ContextPropertyDefinition cpd, Vector d, Vector r) throws ContextException {
        this.propertyDefinition = cpd;
        Object o;
        ContextualInformation ci;
        this.domains = new Vector();
        for (Enumeration e = d.elements(); e.hasMoreElements();)
        {
            o = e.nextElement();
            if (o instanceof ContextualInformation)
            {
                ci = (ContextualInformation) o;
                domains.addElement(ci);
            }
            else throw new ContextException("Unable to create context property, non contextual information found in data domains.");
        }
        this.ranges = new Vector();
        for (Enumeration e = r.elements(); e.hasMoreElements();)
        {
            o = e.nextElement();
            if (o instanceof ContextualInformation)
            {
                ci = (ContextualInformation) o;
                ranges.addElement(ci);
            }
            else throw new ContextException("Unable to create context property, non contextual information found in data ranges.");
        }
        if (!cpd.validate(this))
            throw new ContextException("Property doesn't match context property definition");
        
    }
    
    public java.util.Enumeration getRanges()        { return ranges.elements(); }
    public java.util.Enumeration getDomains()       { return domains.elements(); }
    public ContextualInformation getRange(int i)    { return (ContextualInformation) ranges.elementAt(i); }
    public ContextualInformation getDomain(int i)   { return (ContextualInformation) domains.elementAt(i); }
    public ContextPropertyDefinition getPropertyDefinition() {
        return propertyDefinition;
    }
    
    public String toXML() {
        String xml = "<ContextProperty name=\""+this.getPropertyDefinition().getName()+"\">\n";
        xml += "\t<DomainInstances>\n";
            for (Enumeration doms = domains.elements(); doms.hasMoreElements();)
                xml  += "\t\t<DomainInstance>\n\t\t"+((ContextualInformation) doms.nextElement()).toResumedXML()+"\t</DomainInstance>\n";
        xml += "\t</DomainInstances>\n";
        xml += "\t<RangeInstances>\n";
            for (Enumeration rans = ranges.elements(); rans.hasMoreElements();)
                xml  += "\t\t<RangeInstance>\n\t\t"+((ContextualInformation) rans.nextElement()).toResumedXML()+"\t</RangeInstance>\n";
        xml += "\t</RangeInstances>\n";
        
        xml += "\t</ContextProperty>\n";
        
        return xml;
    }
    
}
